import React from 'react';
import {render, bindAction, bindModel, getAction, getModel} from 'react-division';
import Model from './Model';
import Action from './Action';
import ConfigTable from './table';


export default ({modelName, actionName}) => {

    // 如果没有绑定过  就绑定默认的
    if (!getModel(modelName)) {
        bindModel({
            [modelName]: Model
        });
    }
    if (!getAction(actionName)) {
        bindAction({
            [actionName]: Action
        });
    }

    return render({
        models: [modelName],
        actions: [actionName]
    })(class extends React.Component {
        constructor(props) {
            super(props);

            props.action[actionName].init(modelName, actionName);
        }   

        render() {
            const model = this.props[modelName];
            const {action, operation, pagination = false} = this.props;

            let columns = action[actionName].processColumns(model) || model.columns;
            let data = action[actionName].processData(model) || model.data;

            return (
                <ConfigTable 
                    pagination={pagination}
                    operation={operation}
                    columns={columns}
                    dataSource={data}
                    onChange={action[actionName].changeTableValue}
                    modelName={modelName}
                    status={model.status}
                    model={model}
                    scrollHeight={model.scrollHeight}
                    onRow={action[actionName].onRow}
                    onFocus={action[actionName].onFocus}
                    renderCell={action[actionName].renderCell}
                />
            );
        }
    });

}